<?php
require_once('lanceur_spip.php');
require_once(_DIR_PLUGIN_ECATALOGUE.'classes/class.CurrencyModel.php');
require_once(_DIR_PLUGIN_PDK.'classes/simpletest/class.PdkModelTest.php');
require_once(_DIR_PLUGIN_ECATALOGUE.'classes/class.RubriqueCurrencyModel.php');
require_once(_DIR_PLUGIN_PDK.'classes/models/PdkRubriqueModel.php');
require_once(_DIR_PLUGIN_PDK.'classes/models/PdkArticleModel.php');

class testClassCurrencyModel extends PdkModelTest {
    /**
     *  RubriqueCurrencyModel
     */
    protected $rub_cur_mdl;
    
    /**
     *  PdkArticleModel
     */
    protected $rub_mdl;
    
    /**
     *  PdkRubriqueModel
     */
    protected $art_mdl;
    
    public function __construct(){
        $this->rub_cur_mdl = new RubriqueCurrencyModel();
        $this->pdk_models['RubriqueCurrencyModel'] = &$this->rub_cur_mdl;
        $this->rub_mdl = new PdkRubriqueModel();
        $this->pdk_models['PdkRubriqueModel'] = &$this->rub_mdl;        
        $this->art_mdl = new PdkArticleModel();
        $this->pdk_models['PdkArticleModel'] = &$this->art_mdl;
        // Call parent constructor with tested model instance
        parent::__construct(new CurrencyModel($this->rub_cur_mdl, $this->rub_mdl, $this->art_mdl));
    }
    
    /**
     *  Run before each test
     */
    function setUp() {
        parent::setUp();    
    }
    
    /**
     *  Run after each test
     */
    function tearDown() {
        parent::tearDown();
    }
    
    function __destruct() {
        // Call parent destructor to delete created tables
        parent::__destruct();
    }
    
    function testGetCurrencyByIdRubrique() {
        // Here we clear standart test data and add our own
        $this->test_table->ClearTables($this->rub_cur_mdl);
        $data = array('id_rubrique'=>1, 'id_currency'=>1);
        $this->rub_cur_mdl->insert($data);
        
        // Rubrique and currency have direct link
        $currency_sel = $this->model->GetCurrencyByIdRubrique(1);
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // Currency inheritance from parent rubrique        
        // rubrique 2 lies in  rubrique 1, so it must have the same currency
        $currency_sel = $this->model->GetCurrencyByIdRubrique(2);        
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // Deeper inheritance
        // rubrique 3 lies in rubrique 1 branch, so it must have the same currency
        $currency_sel = $this->model->GetCurrencyByIdRubrique(3);
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // No currency
        // Rubrique 4 hasn't got any currency
        $currency_sel = $this->model->GetCurrencyByIdRubrique(4);
        $this->assertFalse($currency_sel);
    }  
    
    function testGetCurrencyByIdArticle() {
        // Here we clear standart test data and add our own
        $this->test_table->ClearTables($this->rub_cur_mdl);        
        $data = array('id_rubrique'=>1, 'id_currency'=>1);
        $this->rub_cur_mdl->insert($data);
                
        // Article parent rubrique and currency have direct link
        $currency_sel = $this->model->GetCurrencyByIdArticle(1);
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // Currency inheritance from parent rubrique        
        // rubrique 2 lies in  rubrique 1, so article wich lies in rubrique 2 must have the same currency
        $currency_sel = $this->model->GetCurrencyByIdArticle(2);
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // Deeper inheritance
        // rubrique 3 lies in rubrique 1 branch, so article wich lies in rubrique 3 must have the same currency
        $currency_sel = $this->model->GetCurrencyByIdArticle(3);
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // No currency
        // Rubrique 4 hasn't got any currency so article wich lies in rubrique 4
        $currency_sel = $this->model->GetCurrencyByIdArticle(4);
        $this->assertFalse($currency_sel);
    }
    
    public function testGetCurrencySignByIdArticle() {
        // Here we clear standart test data and add our own
        $this->test_table->ClearTables($this->rub_cur_mdl);        
        $data = array('id_rubrique'=>1, 'id_currency'=>1);
        $this->rub_cur_mdl->insert($data);
        
        // article 1 lies in rubrique 1, so it has currency 1 signs 
        $sign_sel = $this->model->GetCurrencySignByIdArticle(1);
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdArticle(1,'sign_pre');
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdArticle(1,'sign_after');
        $this->assertEqual($this->model->test_data[0]['sign_after'], $sign_sel);
        
        // article 2 lies in rubrique 2, wich lies in rubrique 1, so article 1 has currency 1 signs
        $sign_sel = $this->model->GetCurrencySignByIdArticle(2);
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdArticle(2,'sign_pre');
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdArticle(2,'sign_after');
        $this->assertEqual($this->model->test_data[0]['sign_after'], $sign_sel);
    }
    
    /*
    function testGetCurrencySignByIdCurrency() {
        // Insert test currencies
        $this->model->insert($this->model->test_data[0]);
        
        $sign_sel = $this->model->GetCurrencySignByIdCurrency(1);
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdCurrency(1,'sign_pre');
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdCurrency(1,'sign_after');
        $this->assertEqual($this->model->test_data[0]['sign_after'], $sign_sel);
    }*/
}
